package com._21cn.fetch.utils;

import com._21cn.framework.http.HttpCaller;
import com._21cn.framework.utils.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Thinkpad on 2017/4/25.
 */
public class Fetch020 {
    private static String url = "http://120.77.218.241:9000/cms/article";


    private static String callback = "callback";

    public String getContent(String id){
        String data = "{\"id\":\"{id}\",\"pageNow\":\"30\"}";  //分页
        data = data.replace("{id}",id);
        String rurl = url + "?data="+data+"&callback="+callback;
        HttpCaller httpCaller = HttpCaller.post(url).addParam("data",data).addParam("callback", callback);
        String content = httpCaller.call().getString();
        content = content.replace("callback(","");
        content = content.substring(0, content.length()-1);
        return content;
    }

    public String getCompleteUrl(String srcUrl){
        String completeUrl = srcUrl;
        if (!srcUrl.contains("http://") &&!srcUrl.contains("https://") && srcUrl.contains("ueditor/jsp/")) {
            String domain = "http://120.77.218.241";
            completeUrl = domain +srcUrl;
        }else if(!srcUrl.contains("http://") &&!srcUrl.contains("https://") && srcUrl.contains("attached/image")){
            String domain = "http://120.77.218.241:38080";
            completeUrl = domain +srcUrl;
        }
        return completeUrl;
    }

    public Map<String, String> extractPictureListFromContent(String content) {
        String fetUrl = "http://admin.cms.qiaoliba.com/pictureFetch.do?status=1";
        Map<String, String> resultMap = new HashMap<String, String>();
        if(!StringUtil.isEmpty(content)){
            Document doc = Jsoup.parse(content);
            Elements imgs = doc.getElementsByTag("img");
            int i = 0;
            String urls = "";
            for (Element img: imgs) {
                String sourceUrl = img.attr("src").trim();     // 图片链接
                sourceUrl = getCompleteUrl(sourceUrl);
                urls = urls+",21cn.com,"+sourceUrl;
            }
            if(!StringUtil.isEmpty(urls)){
                urls=urls.substring(",21cn.com,".length(), urls.length());
            }
            String json = HttpCaller.post(fetUrl).addParam("url", urls).call().getString();
            JSONObject array = JSONObject.parseObject(json);
            for (Element img: imgs) {
                String sourceUrl = img.attr("src").trim();     // 图片链接
                sourceUrl = getCompleteUrl(sourceUrl);
                JSONObject obj = array.getJSONObject(sourceUrl);
                String url = obj.getString("url");
                resultMap.put(sourceUrl, url);
            }
        }
        return resultMap;
    }

    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream("E:\\data\\tmp\\id.txt"), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String line = "";
        try {
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                String html = new Fetch020().getContent(line)
                        .replace("<hr style=\"page-break-after:always;\" class=\"ke-pagebreak\" />","<p class=\"pager\">&nbsp;</p>")
                        .replace("<hr style=\"page-break-after:always;\" class=\"ke-pagebreak\">", "<p class=\"pager\">&nbsp;</p>");
                JSONObject jsonObject = JSONObject.parseObject(html);
                JSONObject obj = jsonObject.getJSONObject("article");
                String title = obj.getString("title");
                String content = obj.getJSONObject("articleData").getString("content");
                int mainColumnId = 3;           //// TODO: 2017/4/26
                String sourceName = "广州网";
                String postUrl = "http://admin.cms.qiaoliba.com/admin/article/article/save.do";
                try{
                    Map<String, String> urlmap = new Fetch020().extractPictureListFromContent(content);
                    for(String url : urlmap.keySet()){
                        content = content.replace(url, urlmap.get(url));
                    }
                    HttpCaller.post(postUrl).addParam("title", StringUtil.utf82iso(title)).addParam("content", StringUtil.utf82iso(content))
                            .addParam("sourceName",StringUtil.utf82iso(sourceName)).addParam("mainColumnId", mainColumnId+"")
                            .addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                            .addHeader("Accept-Encoding","gzip, deflate")
                            .addHeader("Content-Type","application/x-www-form-urlencoded").call();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
